/**
 * 
 */
package com.carnation.qa.socketserver.proxy.moi;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.mong.platform.domain.MOIMRelation;
import com.carnation.mong.platform.domain.MOIModel;
import com.carnation.mong.platform.domain.MOIRelationType;
import com.carnation.mong.platform.service.MRelationService;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.Builder;

/**
 * @author Nick
 * 
 */
public class MRelationServiceImpl extends AbstractMOIServiceImpl {

	private final Log log = LogFactory.getLog(getClass());

	private MRelationService service;

	public MRelationServiceImpl(List<Item> items) {
		super(items);
		service = ServiceFactory.getService(MRelationService.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.carnation.qa.socketserver.proxy.AbstractServiceImpl#invoke(com.carnation
	 * .qa.socketserver.domain.Item)
	 */
	@Override
	protected String invoke() {
		Item item = getItems().get(0);
		String method = getMethodName(item);
		String returnStr;
		if ("create".equals(method)) {
			returnStr = create(item);
		} else if ("delete".equals(method)) {
			returnStr = delete(item);
		} else if ("getAllRelations".equals(method)) {
			returnStr = getAllRelations(item);
		} else if ("getMRelation".equals(method)) {
			returnStr = getMRelation(item);
		} else if ("getRelations".equals(method)) {
			returnStr = getRelations(item);
		} else if ("update".equals(method)) {
			returnStr = update(item);
		} else {
			throw new IllegalArgumentException(method);
		}
		return returnStr;
	}

	private String create(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIMRelation i = instanceMOIMRelation(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("create MOIMRelation:" + i);
			}
			try {
				Object obj = service.create(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String delete(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIMRelation i = instanceMOIMRelation(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("delete MOIMRelation:" + i);
			}
			try {
				service.delete(i);
				Object obj = true;
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getAllRelations(Item item) {
		if (log.isDebugEnabled()) {
			log.debug("getAllRelations");
		}
		try {
			Object obj = service.getAllRelations();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getMRelation(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			String dataId = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("getMRelation id:" + dataId);
			}
			try {
				Object obj = service.getMRelation(dataId);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		} else if (item.getChildren().size() == 3) {// 参数个数
			MOIModel srcModel = instanceMOIModel(item.getChildren().get(0));
			MOIModel tarModel = instanceMOIModel(item.getChildren().get(1));
			MOIRelationType rt = instanceMOIRelationType(item.getChildren()
					.get(2));
			if (log.isDebugEnabled()) {
				log.debug("getMRelation srcModel:" + srcModel + ",tarModel:"
						+ tarModel + ",rt:" + rt);
			}
			try {
				Object obj = service.getMRelation(srcModel, tarModel, rt);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getRelations(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIRelationType i = instanceMOIRelationType(item.getChildren().get(
					0));
			if (log.isDebugEnabled()) {
				log.debug("getRelations rt:" + i);
			}
			try {
				Object obj = service.getRelations(i);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String update(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			MOIMRelation i = instanceMOIMRelation(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("update MOIMRelation:" + i);
			}
			try {
				service.update(i);
				Object obj = true;
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

}
